package com.sebklebes.radar.gae.dao.impl;

import com.beoui.geocell.GeocellManager;
import com.beoui.geocell.GeocellUtils;
import com.beoui.geocell.model.GeocellQuery;
import com.beoui.geocell.model.Point;
import com.sebklebes.radar.gae.dao.RadarDAO;
import com.sebklebes.radar.gae.entity.RadarEntity;

import javax.jdo.PersistenceManager;
import java.util.ArrayList;
import java.util.List;

/**
 * User:  catalin.besleaga
 * Date: 5/16/11
 * Time: 10:45 PM
 */
public class RadarDAOImpl extends GenericDAOImpl<RadarEntity> implements RadarDAO {

	/**
	 * {@inheritDoc}
	 */
	public List<RadarEntity> getRadars(Double latitude, Double longitude, Double rangeInMeters, int maxNumberOfResults) {
		List<RadarEntity> radars;
		PersistenceManager pm = pmf.getPersistenceManager();

		Point center = new Point(latitude, longitude);

		List<Object> params = new ArrayList<Object>();
		GeocellQuery baseQuery = new GeocellQuery("", "", params);
		radars = GeocellManager.proximityFetch(center, maxNumberOfResults, rangeInMeters, RadarEntity.class, baseQuery, pm);

		for (RadarEntity radar : radars) {
			radar.setDistance(GeocellUtils.distance(new Point(latitude, longitude), radar.getLocation()));
		}

		pm.close();
		return radars;
	}
}
